package org.xb.ObjectClone;

/**
 * @Author
 * @Description
 * @Date created in 14:54 2023/12/7
 */
public class DeepCopyExample {
    public static void main(String[] args) throws CloneNotSupportedException {
        Address address = new Address("CityA");
        Person originalPerson = new Person("John", address);

        // 浅拷贝
        Person shallowCopy = originalPerson.clone();

        // 深拷贝
        Person deepCopy = new Person(originalPerson.name, new Address(originalPerson.address.city));

        // 修改原始对象的引用类型成员变量
        originalPerson.address.city = "CityB";

        System.out.println("Original: " + originalPerson.address.city);  // CityB
        System.out.println("Shallow Copy: " + shallowCopy.address.city); // CityB
        System.out.println("Deep Copy: " + deepCopy.address.city);       // CityA
    }
}
